const http = require('../../network/http')
const api = require('../../network/api')
var util = require('../../utils/dateTime.js');
// pages/rechargeFlow/rechargeFlow.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    storeId: '',
    merchantId: '',
    orderList: [],
    chooseMonth: '2022-10',
    toMonth: '',
    chooseYear: '',
    toYear: '',
    totalPrice: '',
    pageNumber: 1,
    pages: 0,
    dateType: 1,
    sendDate: '',
    fresh: 0,
  },
   /**
   * 订单列表
   */
  loadincome() {
    http.request({
      showLoading: true,
      url: api.loadRechargeList,
      method: "GET",
      data: {
        storeId: this.data.storeId,
        // storeId: '1594277494833885186', 
        date: this.data.sendDate,
        mode: this.data.dateType,
        pageNumber: this.data.pageNumber,
        pageSize: 20,
      },
    }).then(res => {
      if (res.data.code == 200) {
        console.log("订单列表数据返回:");
        console.log(res.data.result);
        var newList = this.data.orderList;
        if(res.data.result.records != null && res.data.result.records.length > 0){
          for(var i=0; i < res.data.result.records.length; i++){
            newList.push(res.data.result.records[i]);
          }
        }
        this.setData({
          'orderList': newList,
          pages: res.data.result.pages,
          // totalPrice: res.data.result[0].monthTotalPrice,
        })
      }
      this.setData({
        fresh: 1,
      })
    }).catch(err => {
      wx.showToast({
        title: err,
        icon: "none",
        duration: 2000
      });
    });
  },
  /**
   * 获取总金额
   */
  loadTotalPrice() {
    http.request({
      showLoading: true,
      url: api.getTotalCharge,
      method: "GET",
      data: {
        storeId: this.data.storeId,
        // storeId: '1594277494833885186', 
        date: this.data.sendDate,
        mode: this.data.dateType,
      },
    }).then(res => {
      if (res.data.code == 200) {
        this.setData({
          totalPrice: res.data.result,
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err,
        icon: "none",
        duration: 2000
      });
    });
  },
  bindYearChange : function(e){
    this.setData({
      chooseYear: e.detail.value,
      sendDate: e.detail.value,
      pageNumber: 1,
      pages: 0,
      'orderList': [],
    });
    this.loadTotalPrice();
    this.loadincome();
  },
  /**
   * 月份选择
   */
  bindMonthChange(e) {
    this.setData({
        chooseMonth: e.detail.value,
        sendDate: e.detail.value,
        pageNumber: 1,
        pages: 0,
        'orderList': [],
    })
    this.loadTotalPrice();
    this.loadincome();
  },
  clickroomdetail: function (e) {
    var $data = e.currentTarget.dataset;
    console.log($data.bean);
    var detail = $data.bean;
    wx.navigateTo({
      url: '/pages/rechargeDetail/rechargeDetail?detail=' + JSON.stringify(detail) + '&storeId=' + this.data.storeId + '&merchantId=' + this.data.merchantId,
    })
  },
  closeview : function(e){
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var date = util.formatTime(new Date()).substring(0, 10);
    this.setData({
      storeId: options.storeId,
      merchantId: options.merchantId,
      dateType: options.dateType,
      chooseMonth: date.substring(0, 7),
      toMonth: date.substring(0, 7),
      chooseYear: date.substring(0, 4),
      toYear: date.substring(0, 4),
      pageNumber: 1,
      pages: 0,
      'orderList': [],
    })
    if(this.data.dateType == 1){
      this.setData({
        sendDate: date.substring(0, 7),
      })
    }else{
      this.setData({
        sendDate: date.substring(0, 4),
      })
    }
    this.loadTotalPrice();
    this.loadincome();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(this.data.fresh == 1){
      var date = util.formatTime(new Date()).substring(0, 10);
      this.setData({
        chooseMonth: date.substring(0, 7),
        toMonth: date.substring(0, 7),
        chooseYear: date.substring(0, 4),
        toYear: date.substring(0, 4),
        pageNumber: 1,
        pages: 0,
        'orderList': [],
      })
      if(this.data.dateType == 1){
        this.setData({
          sendDate: date.substring(0, 7),
        })
      }else{
        this.setData({
          sendDate: date.substring(0, 4),
        })
      }
      this.loadTotalPrice();
      this.loadincome();
    }  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      pageNumber: 1,
      pages: 0,
      'orderList': [],
    })
    this.loadincome();
    setTimeout(function () {
      wx.stopPullDownRefresh();
    }, 500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.pages > this.data.pageNumber){
      var newPage = this.data.pageNumber + 1;
      this.setData({
        pageNumber: newPage,
        pages: 0,
      })
      this.loadincome();
    }else{
      wx.showToast({
        title: '暂无更多数据',
        icon: "none",
        duration: 2000
      });
    }
    wx.pageScrollTo({
      // scrollTop: 0,
      duration: 300,
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})